﻿using System;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Web;
using SyCODE.Component.DataFactory;
using System.Collections.Specialized;
using SyCODE.Component.Security;
using SyCODE.Component;
using System.Web.Security;

/// <summary>
/// SyCODE 的摘要说明
/// </summary>
namespace SyCODE.Blog
{
    public class SetSort : IHttpHandler
    {        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
			Ticket ticket = Ticket.Get(FormsAuthentication.FormsCookieName);
            if (Ticket.Authenticate(ticket))
            {
                NameValueCollection nvc = new NameValueCollection(context.Request.Form);

                string path = Path.Combine(AppPath.GetUserPhysicalPath(ticket.Name), "mysort.xml");

                XmlDocument sortDom = new XmlDocument();
                sortDom.Load(path);

                switch (nvc["active"])
                {
                    case "0":
                        context.Response.Write(Add(sortDom, nvc["name"]));
                        break;
                    case "1":
                        Rename(sortDom, nvc["sort"], nvc["name"]);
                        break;
                    case "2":
                        Remove(sortDom, nvc["sort"]);
                        nvc.Add("u", ticket.Name);

                        DBAM dbam = new DBAM();
                        dbam.BuildParameters("deletesort", nvc);
                        try
                        {
                            dbam.Execute();
                        }
                        finally
                        {
                            dbam.Dispose();
                        }
                        break;
                    default:
                        break;
                }
                sortDom.Save(path);
            }
            else
            {
                Function.ClientAlert(StringResource.GetField("authfaild"));
            }
        }

        private string Add(XmlDocument sortDom, string name)
        {
            XmlNode body = sortDom.DocumentElement.SelectSingleNode("body");
            string id = (Int32.Parse(body.LastChild.Attributes["id"].Value) + 1).ToString();

            XmlElement sort = sortDom.CreateElement("a");
            sort.SetAttribute("id", id);
            sort.SetAttribute("href", String.Format("sort/{0}.xml",  id));
            sort.InnerText = name;

            body.AppendChild(sort);

            return id;
        }

        private void Rename(XmlDocument sortDom, string id, string name)
        {
            sortDom.DocumentElement.SelectSingleNode(String.Format("body/a[@id='{0}']", id)).InnerText = name;
        }

        private void Remove(XmlDocument sortDom, string id)
        {
            if (Int32.Parse(id) > 1)
            {
                XmlNode body = sortDom.DocumentElement.SelectSingleNode("body");
                body.RemoveChild(body.SelectSingleNode(String.Format("a[@id='{0}']", id)));
            }
        }
    }
}